Mysql数据库 | 您所在的位置:网站首页 › table 软件 › Mysql数据库 |
本文已参与「新人创作礼」活动,一起开启掘金创作之路。 DML数据操作语言,凡是对数据进行增删改操作的语句都是DML insert delete update insert 增 delete 删 update 改 这个主要操作表中的data 数据表记录的插入 插入完整记录 mysql> desc xs; +----------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+------------------+------+-----+---------+-------+ | 学号 | char(6) | NO | PRI | NULL | | | 姓名 | char(8) | NO | | NULL | | | 专业名 | varchar(20) | YES | | NULL | | | 性别 | char(2) | NO | | 男 | | | 出生日期 | date | NO | | NULL | | | 总学分 | tinyint unsigned | YES | | NULL | | | 备注 | text | YES | | NULL | | +----------+------------------+------+-----+---------+-------+ 7 rows in set (0.01 sec) mysql> insert into xs values('200201','张明','计算机应用技术','男','1998-8-5','9','学习委员'); Query OK, 1 row affected (0.01 sec) 复制代码 插入数据就录的一部分 mysql> insert into xs(学号,姓名,性别,出生日期) values('200326','唐辉阳','男','2003-2-6'); Query OK, 1 row affected (0.01 sec) 复制代码插入多行数据 mysql> insert into xs(学号,姓名,性别,出生日期) -> values -> ('200336','李昌城','男','2003-6-3'), -> ('200327','徐斌','男','2003-3-5'), -> ('200308','郑明鑫','男','2003-4-5'); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 复制代码 数据表记录的修改 mysql> update xs -> set 姓名='魏硕' where 学号='200201'; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 复制代码 数据表记录的删除 使用delete语句删除数据 mysql> delete from xs -> where 姓名='高远'; Query OK, 1 row affected (0.01 sec) 复制代码使用truncate语句删除数据 (清空数据记录) mysql> select * from sss; +-------+-------+---------+------+--------+ | uid | uname | ugender | uedu | upor | +-------+-------+---------+------+--------+ | 11111 | tang | n | bk | | | 22222 | zhang | n | bk | 副教授 | +-------+-------+---------+------+--------+ 2 rows in set (0.00 sec) mysql> truncate sss; Query OK, 0 rows affected (0.04 sec) mysql> select * from sss; Empty set (0.01 sec) 复制代码 DDL数据定义语言,凡是带create,drop,alter的都是DDL DDL主要操作的是表的结构,不是表中的数据 create 增 drop 删 alter 改 这个增删改和DML不同,主要是对表的结构进行操作 alter 改 修改数据表名 alter table cj rename 成绩表; 复制代码 修改字段名和数据类型语法格式: alter table 表名 change 旧字段名 新字段名 新数据类型; 复制代码 alter table 信息管理学生表 change 专业名 zym varchar(15); 复制代码 修改字段的数据类型语法格式: alter table 表名 modify 字段名 新数据类型; 复制代码 alter table 信息管理学生表 modify zy varchar(20); 复制代码 添加字段语法格式: alter table 表名 add 新字段名 新数据类型 [约束条件] [first | after 已经存在的字段名]; 复制代码 alter table 信息管理学生表 add 年龄 varchar(3); 复制代码 删除字段语法格式: alter table 表名 drop 字段名; 复制代码 alter table xs drop 备注; 复制代码 create 增 根据查询创建数据表 create table 软件技术 select 学号,姓名,性别,出生日期,总学分,备注 from xs where 专业名='软件技术'; 复制代码 drop 删 删除表 create table zyrs like xs; drop table zyrs; 复制代码 TCL事务控制语言 包括: 事务提交:commit; 事务回滚:rollback; 复制代码 DCL数据控制语言 例如:授权grant、撤销权限revoke…… 导入数据库 mysql> show databases; +--------------------+ | Database | +--------------------+ | cjgl | | cjgl1 | | information_schema | | mysql | | performance_schema | | sys | | tang | +--------------------+ 7 rows in set (0.00 sec) mysql> use cjgl1; Database changed mysql> source D:\Program Files\QQ files\cjgl-bak.sql Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.04 sec) Query OK, 0 rows affected (0.04 sec) Query OK, 0 rows affected (0.05 sec) Query OK, 0 rows affected (0.05 sec) Query OK, 29 rows affected (0.05 sec) Records: 29 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected (0.06 sec) Query OK, 0 rows affected (0.06 sec) Query OK, 0 rows affected (0.06 sec) Query OK, 0 rows affected (0.06 sec) Query OK, 0 rows affected (0.06 sec) 复制代码 数据处理函数数据处理函数又被称为单行处理函数 单行处理函数的特点:一个输入对应一个输出 和单行处理函数相对的是:多行处理函数(多行处理函数的特点:多个输入,对应一个输出!) 单行处理函数 lower 转换小写 mysql> select lower(课程名) 课程名,开课学期 from kc where 课程号=212; +--------------+----------+ | 课程名 | 开课学期 | +--------------+----------+ | oracle数据库 | 2 | +--------------+----------+ 1 row in set (0.01 sec) mysql> select 课程名,开课学期 from kc where 课程号=212; +--------------+----------+ | 课程名 | 开课学期 | +--------------+----------+ | ORACLE数据库 | 2 | +--------------+----------+ 1 row in set (0.00 sec) 复制代码 upper 转换大写 mysql> select upper(课程名) 课程名,开课学期 from kc; +--------------+----------+ | 课程名 | 开课学期 | +--------------+----------+ | 计算机基础 | 1 | | C语言 | 1 | | 高等数学 | 3 | | 数据结构 | 5 | | 操作系统 | 6 | | 计算机组装 | 4 | | ORACLE数据库 | 2 | | 计算机网络 | 1 | | 软件工程 | 7 | +--------------+----------+ 9 rows in set (0.01 sec) mysql> select 课程名,开课学期 from kc; +--------------+----------+ | 课程名 | 开课学期 | +--------------+----------+ | 计算机基础 | 1 | | c语言 | 1 | | 高等数学 | 3 | | 数据结构 | 5 | | 操作系统 | 6 | | 计算机组装 | 4 | | ORACLE数据库 | 2 | | 计算机网络 | 1 | | 软件工程 | 7 | +--------------+----------+ 9 rows in set (0.00 sec) 复制代码 substr取子串 #模糊查询课程号中第二位为零的行 mysql> select 课程号 from kc where substr(课程号,2,1)='0'; +--------+ 从课程号中第2位开始,取1位 | 课程号 | +--------+ | 102 | | 209 | | 208 | | 101 | | 301 | | 302 | | 206 | +--------+ 7 rows in set (0.00 sec) 复制代码 concat字符串拼接 mysql> select concat('Hello','World') result; +------------+ | result | +------------+ | HelloWorld | +------------+ 1 row in set (0.00 sec) 复制代码 length取长度 mysql> select length('hhgjfgks') as result; +--------+ | result | +--------+ | 8 | +--------+ 1 row in set (0.00 sec) 复制代码 trim去空格 mysql> select trim(' hkhhukhhyiyy ') result; +--------------+ | result | +--------------+ | hkhhukhhyiyy | +--------------+ 1 row in set (0.00 sec) 复制代码 首字母小写 mysql> select concat(lower(substr(课程名,1,1)),substr(课程名,2,length(课程名)-1)) as result from kc where 课程号='212'; +--------------+ | result | +--------------+ | oRACLE数据库 | +--------------+ 1 row in set (0.00 sec) mysql> select 课程名 from kc where 课程号='212'; +--------------+ | 课程名 | +--------------+ | ORACLE数据库 | +--------------+ 1 row in set (0.00 sec) 复制代码 str_to_date将字符串转化成日期 date_format格式化日期 format设置千分位 round四舍五入 mysql> select round(3.6) as result; +--------+ | result | +--------+ | 4 | +--------+ 1 row in set (0.00 sec) mysql> select round(3.764,2) as result; //四舍五入保留两位小数 +--------+ | result | +--------+ | 3.76 | +--------+ 1 row in set (0.00 sec) 复制代码 rand()生成随机数 mysql> select round(rand(),1) 随机数 from kc;//生成0到1之间的随机数,保留一位小数 +--------+ | 随机数 | +--------+ | 0.9 | | 0 | | 0.4 | | 0.8 | | 0.8 | | 0.6 | | 0.5 | | 0.8 | | 0.6 | +--------+ 9 rows in set (0.00 sec) mysql> select round(rand()*100,0) result from kc; //100以内随机数 +--------+ | result | +--------+ | 6 | | 76 | | 60 | | 76 | | 97 | | 58 | | 98 | | 19 | | 98 | +--------+ 9 rows in set (0.00 sec) 复制代码 ifnull函数可以将null转化成一个具体的值 注意: null如果参与运算,最终结果一定是null。为了避免这一现象,需要ifnull函数。 ifnull函数用法: ifnull(数据,被当做那个值) 如果“数据”为null是,那么这个数据结构当做那个值 mysql> select 6 + ifnull(备注,8) result from xs; +--------+ | result | +--------+ | 14 | | 14 | | 14 | | 14 | | 14 | | 14 | | 14 | | 14 | | 6 | | 14 | | 6 | | 14 | | 6 | | 14 | | 14 | | 14 | | 14 | | 14 | | 14 | | 14 | +--------+ 20 rows in set (0.00 sec) 复制代码 case..when..then..when..then..else..end mysql> select 开课学期,学分 from kc; +----------+------+ | 开课学期 | 学分 | +----------+------+ | 1 | 3 | | 1 | 3 | | 3 | 3 | | 5 | 6 | | 6 | 3 | | 4 | 4 | | 2 | 5 | | 1 | 4 | | 7 | 2 | +----------+------+ 9 rows in set (0.00 sec) mysql> select 开课学期,(case 开课学期 when '1' then 学分+1 when '2' then 学分+2 else 学分 end) new from kc; +----------+------+ | 开课学期 | new | +----------+------+ | 1 | 4 | | 1 | 4 | | 3 | 3 | | 5 | 6 | | 6 | 3 | | 4 | 4 | | 2 | 7 | | 1 | 5 | | 7 | 2 | +----------+------+ 9 rows in set (0.00 sec) 复制代码 分组函数(多行处理函数)多行处理函数的特点:输入多行,最终输出一行 count 计数 sum 求和 avg 平均值 max 最大值 min 最小值 |
CopyRight 2018-2019 实验室设备网 版权所有 |